package com.zeng.controller;


import com.zeng.dao.LeagueTableQueryDao;
import com.zeng.entity.Result;
import com.zeng.entity.ResultPage;

import com.zeng.entity.Carts;
import com.zeng.service.CartsService;
import com.zeng.utils.TimeUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * (Carts)表控制层
 *
 * @author zengfanchao
 * @since 2021-03-31 21:27:44
 */
@RestController
@RequestMapping("carts")
public class CartsController {
    /**
     * 服务对象
     */
    @Resource
    private CartsService cartsService;
    @Resource
    private LeagueTableQueryDao leagueTableQueryDao;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Carts selectOne(Integer id) {
        return this.cartsService.queryById(id);
    }

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping("selectAll")
    public List<Carts> selectAll() {
        return this.cartsService.selectAll();
    }

    /**
     * 根据页号查询
     *
     * @param offset 查询初始位置
     * @param limit  查询数量
     * @return
     */
    @GetMapping("selectAllByLimit")
    public ResultPage selectAllByLimit(int offset, int limit) {
        int pageTotal = this.cartsService.queryCount();
        List<Carts> list = this.cartsService.queryAllByLimit(offset, limit);
        return ResultPage.success(pageTotal, "查询成功", list);
    }

    @PostMapping("selectKeyByLimit")
    public ResultPage selectKeyByLimit(@RequestBody HashMap<String, String> map) {
        if (map.get("key").equals("")) {
            int pageTotal = this.cartsService.queryCount();
            List<Carts> list = this.cartsService.queryAllByLimit(Integer.valueOf(map.get("offset")), Integer.valueOf(map.get("limit")));
            return ResultPage.success(pageTotal, "查询成功", list);
        } else {
            int pageTotal = this.cartsService.queryCountSelect(map.get("key"));
            List<Carts> list = this.cartsService.queryKeyByLimit(map.get("key"), Integer.valueOf(map.get("offset")), Integer.valueOf(map.get("limit")));
            return ResultPage.success(pageTotal, "查询成功", list);
        }
    }

    /**
     * 根据主键删除
     *
     * @return
     */
    @GetMapping("deleteById")
    public Result deleteById(Integer id) {
        this.cartsService.deleteById(id);
        return Result.success("删除成功");
    }

    @GetMapping("deleteByFlag")
    public Result deleteByFlag(Integer id) {
        Carts carts = cartsService.queryById(id);
        carts.setDeleteflag(1);
        cartsService.update(carts);
        return Result.success("删除成功");
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody HashMap<String, String> map
    ) {
        Carts carts = this.cartsService.queryById(Integer.valueOf(map.get("cartid")));
        carts.setUid(Integer.valueOf(map.get("uid")));
        carts.setGoodsid(Integer.valueOf(map.get("goodsid")));
        carts.setDeleteflag(Integer.valueOf(map.get("deleteflag")));
        carts.setCreatetime(TimeUtils.NowTimeN());
        cartsService.update(carts);
        return Result.success("修改成功");
    }

    @PostMapping("addCarts")
    public Result addCarts(@RequestBody HashMap<String, String> map
    ) {
        Carts carts = new Carts();
        carts.setUid(Integer.valueOf(map.get("uid")));
        carts.setGoodsid(Integer.valueOf(map.get("goodsid")));
        carts.setDeleteflag(0);
        carts.setCreatetime(TimeUtils.NowTimeN());
        cartsService.insert(carts);
        return Result.success("收藏成功");
    }

    @PostMapping("removeCarts")
    public Result removeCarts(@RequestBody HashMap<String, String> map
    ) {
        Carts carts = new Carts();
        carts.setUid(Integer.valueOf(map.get("uid")));
        carts.setGoodsid(Integer.valueOf(map.get("goodsid")));
        List<Carts> list = this.cartsService.queryAll(carts);
        for (Carts carts1 : list) {
            carts1.setDeleteflag(1);
            this.cartsService.update(carts1);
        }
        return Result.success("取消收藏");
    }

    @PostMapping("haveCarts")
    public Result haveCarts(@RequestBody HashMap<String, String> map
    ) {
        Carts carts = new Carts();
        carts.setUid(Integer.valueOf(map.get("uid")));
        carts.setGoodsid(Integer.valueOf(map.get("goodsid")));
        carts.setDeleteflag(0);
        List<Carts> list = this.cartsService.queryAll(carts);
        if (list.size() > 0)
            return Result.success("已收藏");
        return Result.error("未收藏");
    }

    @GetMapping("getCartsByUid")
    public List<HashMap<String, Object>> getCartsByUid(Integer id
    ) {
        List<HashMap<String, Object>> rlist = this.leagueTableQueryDao.selectCollectionByUId(id);
        return rlist;
    }


}
